package com.lanou3g.dllo.baidumusic.musiclibrary.ranklist;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.main.MainActivity;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;


/**
 * Created by dllo on 16/6/17.
 */
public class RanklistFragment extends BaseFragment {

    private ListView ranklistListView;
    private RanklistAdapter ranklistAdapter;
    private RanklistBean ranklistBean;

    @Override
    public int setLayout() {
        return R.layout.fragment_ranklist;
    }

    @Override
    public void initView(View view) {
        ranklistListView = (ListView) view.findViewById(R.id.ranklist_listview);
        ranklistAdapter = new RanklistAdapter(context);

    }

    @Override
    public void initData() {

        ranklistListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainActivity mainActivity = (MainActivity) getActivity();
                int rankType = ranklistBean.getContent().get(position).getType();
                mainActivity.toRanklistDetail(rankType);
            }
        });


        // 第一步 创建一个请求队列,请求队列 在整个项目里只需要一个
        RequestQueue requestQueue = RanklistSingleQueue.getRanklistSingleQueue(context).getRequestQueue();
        // 第二步 创建请求
        RanklistGsonRequest<RanklistBean> gsonRequest = new RanklistGsonRequest<RanklistBean>(Request.Method.GET, UrlValues.RANK_LIST_URL,
                new Response.Listener<RanklistBean>() {
                    @Override
                    public void onResponse(RanklistBean response) {
                        ranklistBean = response;
                        ranklistAdapter.setRanklistBean(response);;
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }, RanklistBean.class

        );

        requestQueue.add(gsonRequest);
        ranklistListView.setAdapter(ranklistAdapter);

   }

}
